package com.myproject.musicrecommendationsystem.controller;

import com.myproject.musicrecommendationsystem.domain.User;
import com.myproject.musicrecommendationsystem.service.UserService;
import com.myproject.musicrecommendationsystem.util.AjaxResult;
import com.myproject.musicrecommendationsystem.util.JwtUtil;
import com.myproject.musicrecommendationsystem.util.LocalUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RestController
@Api(tags = "用户系统管理(0729单禹铭)")
public class SystemController {
    @Autowired
    private UserService service;
    @Autowired
    private LocalUploadUtil localUploadUtil;
   @PostMapping("/login")
   @ApiOperation("登录")
    public String login(String username, String password){
       User user = service.getUserByUsernameAndPassword(username, password);
       if(user != null){
           log.info("用户登录成功，生成Token！用户名：{}", user.getUsername());
           // 生成JWT Token
           String token = JwtUtil.createToken(user);
           return "登陆成功，获取到token=" + token; // 返回生成的Token
       }else{
           log.info("用户名或密码错误！,登陆失败");
           return "用户名或密码错误！";
       }
   }
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public String register(@RequestBody User dbuser) {
        //判断用户名是否存在
        User user = service.getUserByUsernameAndPassword(dbuser.getUsername(),dbuser.getPassword());
        if(user!=null){
            return "用户名已存在！";
        }
        log.info("接收到的用户名：{}", dbuser.getUsername());
        log.info("接收到的密码：{}", dbuser.getPassword());

        int isSaved = service.insertUser(dbuser);
        if(isSaved !=0){
            log.warn("注册成功,用户名为"+dbuser.getUsername()+"的用户");
            return "注册成功";
        }else{
            return "注册失败";
        }
    }
    @ApiOperation("图片上传")
    @PostMapping("/photoUpload")
    public AjaxResult photoUpload(@RequestPart("file") MultipartFile file) {
        if(file.isEmpty()){
            return AjaxResult.error(400,"上传的文件为空");
        }
        String arr[] = file.getOriginalFilename().split("\\.");
        System.out.println("获取到的文件后缀为"+arr[arr.length-1]);
        String loop =arr[arr.length-1];
        String upload;
        switch (loop){
            case "jpg":
                System.out.println("获取到jpg格式的图片");
                upload = localUploadUtil.upload(file);
                return AjaxResult.success(upload);
            case "jpeg":
                System.out.println("获取到jpeg格式的图片");
                upload = localUploadUtil.upload(file);
                return AjaxResult.success(upload);
            case "png":
                System.out.println("获取到png格式的图片");
                upload = localUploadUtil.upload(file);
                return AjaxResult.success(upload);
            default :
                System.out.println("用户上传了一个"+loop+"文件");
                return AjaxResult.error(401,"抱歉，仅支持jpg/jpeg/png文件");
        }


    }
}
